import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_card.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_search_enter.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/filter_dialog/custom_filter_right_sheet.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/certificate_file_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/compliance_file_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/basic_information/file_management/file_list/file_list_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

/// 合规性文件、证照文件列表页面
class FileListPage extends StatelessWidget {
  FileListPage({super.key});

  final controller = Get.put(FileListController());

  ///获取条件搜索项
  Widget _getSearchConditionWidget() {
    return Row(
      children: [
        Expanded(
            child: CustomSearchEnter(
          height: 32.w,
          hintText: '请输入文件名称',
          text: controller.search,
          onCallBack: (value) {
            controller.search = value;
            controller.refreshPage();
          },
        )),
        AppGaps.hGap8,
        _screen(),
      ],
    );
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 36.w,
      height: 32.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 18.w,
        height: 16.w,
      ),
    ).clickOnce(controller.screen);
  }

  /// 获取状态图标
  Widget _getStateView(String state) {
    // String? stateText;
    Color? color;
    switch (state) {
      case '正常':
        // stateText = '正常';
        color = const Color(0xff5C6DFF);
        break;
      case '临期':
        // stateText = '临期';
        color = const Color(0xffFC9900);
        break;
      case '过期':
        // stateText = '过期';
        color = const Color(0xffFD6B6B);
        break;
    }
    return Container(
      alignment: Alignment.center,
      height: 18.w,
      width: 52.w,
      decoration: BoxDecoration(
        color: color ?? Colors.white,
        borderRadius: BorderRadius.only(
          topRight: Radius.circular(20.w),
          topLeft: Radius.circular(20.w),
          bottomRight: Radius.circular(20.w),
        ),
      ),
      child: Text(
        state,
        style: TextStyle(
          fontSize: 10.sp,
          fontWeight: FontWeight.w400,
          color: Colors.white,
        ),
      ),
    );
  }

  /// 获取字段显示
  Widget _getTextView({required String title, required String value}) {
    return Row(
      children: [
        Text(
          title,
          style: TextStyle(
            fontSize: 14.sp,
            height: 1.2,
            fontWeight: FontWeight.w400,
            color: const Color(0xffA5A6B5),
          ),
        ),
        Expanded(
            child: Container(
          alignment: Alignment.centerRight,
          child: Text(
            value,
            style: TextStyle(
              fontSize: 14.sp,
              height: 1.2,
              fontWeight: FontWeight.w400,
              color: const Color(0xff495061),
            ),
          ),
        ))
      ],
    );
  }

  ///获取右侧筛选样式
  Widget _getFilterWidget() {
    return CustomFilterRightSheet(
      dataList: controller.filterList,
      onConfirm: controller.screenConfirm,
      isRadio: true,
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            appBar: CustomAppBar(
              titleText: controller.pageType == 0 ? '合规性文件' : '证照文件',
            ),
            key: controller.popRightScaffoldKey,
            endDrawer: Container(
              width: AppDimens.sw - 70.w,
              height: AppDimens.sh,
              color: Colors.white,
              child: _getFilterWidget(),
            ),
            body: Container(
              color: const Color(0xffF5F5F6),
              padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.w),
              child: Column(
                children: [
                  _getSearchConditionWidget(),
                  AppGaps.vGap12,
                  Expanded(
                      child: controller.pageType == 0
                          //合规性文件
                          ? CustomEasyRefreshList<ComplianceFileEntity>(
                              key: controller.globalKey,
                              dataLoad: (pageIndex, pageSize, {String? search}) async {
                                return controller.getComplianceListData(pageIndex, pageSize);
                              },
                              itemBuilder: (ComplianceFileEntity item, int index) {
                                return CustomCard(
                                  onTap: () {
                                    // 点击进入详情
                                    Get.toNamed(AppRoutes.complianceFileDetails, arguments: item);
                                  },
                                  padding: EdgeInsets.zero,
                                  titleWidget: Padding(
                                    padding: EdgeInsets.all(14.w),
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        SvgPicture.asset(AppImages.icon_title),
                                        AppGaps.hGap10,
                                        Expanded(
                                          child: Text(
                                            item.fileName!,
                                            style: TextStyle(
                                              fontSize: 15.sp,
                                              fontWeight: FontWeight.w500,
                                              color: const Color(0xff333333),
                                            ),
                                          ),
                                        ),
                                        _getStateView(item.statusName!)
                                      ],
                                    ),
                                  ),
                                  divider: CustomCard.builtinDivider,
                                  content: Padding(
                                    padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 16.w),
                                    child: Column(
                                      children: [
                                        _getTextView(title: '编制时间：', value: item.organizationDate!),
                                        AppGaps.vGap12,
                                        _getTextView(title: '有效期：', value: item.effectiveDate!),
                                      ],
                                    ),
                                  ),
                                );
                              },
                            )
                          //证照文件
                          : CustomEasyRefreshList<CertificateFileEntity>(
                              key: controller.globalKey,
                              dataLoad: (pageIndex, pageSize, {String? search}) async {
                                return controller.getCertificateListData(pageIndex, pageSize);
                              },
                              itemBuilder: (CertificateFileEntity item, int index) {
                                return CustomCard(
                                  onTap: () {
                                    // 点击进入详情
                                    Get.toNamed(AppRoutes.certificateFileDetails, arguments: item.id);
                                  },
                                  padding: EdgeInsets.zero,
                                  titleWidget: Padding(
                                    padding: EdgeInsets.all(14.w),
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        SvgPicture.asset(AppImages.icon_title),
                                        AppGaps.hGap10,
                                        Expanded(
                                          child: Text(
                                            item.certificateName!,
                                            style: TextStyle(
                                              fontSize: 15.sp,
                                              fontWeight: FontWeight.w500,
                                              color: const Color(0xff333333),
                                            ),
                                          ),
                                        ),
                                        _getStateView(item.statusName!)
                                      ],
                                    ),
                                  ),
                                  divider: CustomCard.builtinDivider,
                                  content: Padding(
                                    padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 16.w),
                                    child: Column(
                                      children: [
                                        _getTextView(title: '持证对象：', value: item.fileObjectName!),
                                        AppGaps.vGap12,
                                        _getTextView(title: '颁发日期：', value: item.obtainDate!),
                                        AppGaps.vGap12,
                                        _getTextView(title: '到期日期：', value: item.becomeDueDate!),
                                      ],
                                    ),
                                  ),
                                );
                              },
                            ))
                ],
              ),
            ),
          );
        });
  }
}
